package com.hgy.carsb.service;

import com.hgy.carsb.entity.IndicatorDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class MainIndicatorService {

    @Autowired
    private InfoService infoService;

    public IndicatorDetail getSignalDetail(int year, int month, String zbCode, String prefix){
        String thisMonth = infoService.getThisMonth(year, month, zbCode, prefix);
        String thisYear = thisMonth;
        String lastYear = infoService.getThisMonth(year-1, month, zbCode, prefix);
        String lastMonth;
        if (month==1){
            lastMonth = infoService.getThisMonth(year-1, 12, zbCode, prefix);
        }else {
            lastMonth = infoService.getThisMonth(year, month-1, zbCode, prefix);
        }
        return getDetail(thisMonth, lastMonth, thisYear, lastYear);
    }

    public IndicatorDetail getOverlayDetail(int year, int month, String zbCode, String prefix){
        String thisMonth = infoService.getThisMonth(year, month, zbCode, prefix);
        String lastMonth;
        if (month==1){
            lastMonth = infoService.getThisMonth(year-1, 12, zbCode, prefix);
        }else {
            lastMonth = infoService.getThisMonth(year, month-1, zbCode, prefix);
        }
        String thisYear = infoService.getYearSum(year, month, zbCode, prefix);
        String lastYear = infoService.getYearSum(year-1, month, zbCode, prefix);
        return getDetail(thisMonth, lastMonth, thisYear, lastYear);
    }

    public IndicatorDetail getRatioDetail(int year, int month, String code1, String code2, String prefix){
        String thisMonth = infoService.getRatio(year, month, code1, code2, prefix);
        String thisYear = thisMonth;
        String lastMonth;
        if (month==1){
            lastMonth = infoService.getRatio(year-1, 12, code1, code2, prefix);
        }else {
            lastMonth = infoService.getRatio(year, month-1, code1, code2, prefix);
        }
        String lastYear = infoService.getRatio(year-1, month, code1, code2, prefix);
        return getDetail(thisMonth, lastMonth, thisYear, lastYear);
    }
    private IndicatorDetail getDetail(String thisMonth, String lastMonth, String thisYear, String lastYear){
        IndicatorDetail indicatorDetail = new IndicatorDetail();
        indicatorDetail.setThisMonth(thisMonth);
        indicatorDetail.setLastMonth(lastMonth);
        indicatorDetail.setThisYear(thisYear);
        indicatorDetail.setLastYear(lastYear);
        BigDecimal thisMonthNum = new BigDecimal(thisMonth);
        BigDecimal lastMonthNum = new BigDecimal(lastMonth);
        BigDecimal thisYearNum = new BigDecimal(thisYear);
        BigDecimal lastYearNum = new BigDecimal(lastYear);
        indicatorDetail.setSubMonth(thisMonthNum.subtract(lastMonthNum).toString());
        indicatorDetail.setSubYear(thisYearNum.subtract(lastYearNum).toEngineeringString());
        return indicatorDetail;
    }
}
